package cn.yangyumanage.model;

import android.util.Log;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import cn.yangyumanage.application.DataApplication;
import cn.yangyumanage.bean.Film;
import cn.yangyumanage.bean.LeaveMessage;
import cn.yangyumanage.bean.LeaveMessageWithUserInfo;
import cn.yangyumanage.bean.Page;
import cn.yangyumanage.bean.util.NormalContainer;
import cn.yangyumanage.bean.util.PageContainer;
import cn.yangyumanage.callback.DataCallBack;
import cn.yangyumanage.callback.HttpCallBack;
import cn.yangyumanage.config.IpConfig;
import cn.yangyumanage.http.HttpClient;
import cn.yangyumanage.util.Aes;

public class LeaveMessageHttpModel implements BaseModel {

    //通过id更新留言数据
    public void updateLeaveMessageById(LeaveMessageWithUserInfo leaveMessage, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/leavemessages";
        String json = DataApplication.gson.toJson(leaveMessage);
        HttpClient.asyncPut(url, json, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    NormalContainer container = DataApplication.gson.fromJson(decrypt, NormalContainer.class);
                    dataCallBack.getData(container.getFlag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //根据留言id删除留言
    public void deleteLeaveMessageById(Integer id, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/leavemessages/" + id;
        HttpClient.asyncDelete(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    NormalContainer container = DataApplication.gson.fromJson(decrypt, NormalContainer.class);
                    dataCallBack.getData(container.getFlag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.ADMIN);
    }

    //根据留言状态分页查询留言及其用户信息
    public void getLeaveMessage(int state, int currentPage, int pageSize, DataCallBack dataCallBack) {
        String url = IpConfig.AGREEMENT + IpConfig.SERVERIP + "/public/leavemessages/" + state + "/" + currentPage + "/" + pageSize;
        HttpClient.asyncGet(url, new HttpCallBack() {
            @Override
            public void onSuccess(String msg) {
                try {
                    String decrypt = Aes.decrypt(msg);
                    TypeToken<PageContainer<LeaveMessageWithUserInfo>> typeToken = new TypeToken<PageContainer<LeaveMessageWithUserInfo>>() {
                    };
                    PageContainer<LeaveMessageWithUserInfo> pageContainer = DataApplication.gson.fromJson(decrypt, typeToken.getType());
                    Page<LeaveMessageWithUserInfo> data = pageContainer.getData();
                    dataCallBack.getData(data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }
        }, HttpClient.PUBLIC);
    }

    @Override
    public void onDestroy() {

    }
}
